/**
 * 浅拷贝转换
 * @param {*} obj 需要转换的对象
 * @param {*} TransormStr 转换的字段,格式:{原字段:新字段},例如:"{userName:name,phone:tel,userId:id}"
 * @param {*} isRetainUntransorm boolean 是否保留未转换的字段
 */
function TransormObjKey(obj = null, TransormStr, isRetainUntransorm = true) {
    if (!obj) {
        return null;
    }
    if (!TransormStr) {
        return obj;
    } else {
        if (isRetainUntransorm) {
            let newObj = Object.keys(obj).reduce((newdata, key) => {
                let newKey = TransormStr[key] || key;
                newdata[newKey] = obj[key];
                return newdata;
            }, {})
            return newObj;
        } else {
            let newObj = Object.keys(TransormStr).reduce((newdata, key) => {
                let newKey = TransormStr[key];
                newdata[newKey] = obj[key];
                return newdata;
            }, {})
            return newObj;
        }
    }
}


const isType = type => (/^\[object\s(.*)\]$/.exec(Object.prototype.toString.call(type)))[1];

function IsNull(obj) {
    return isType(obj).toLowerCase() === 'null';
}

function IsString(obj) {
    return isType(obj).toLowerCase() === 'string';
}

function IsObject(obj) {
    return isType(obj).toLowerCase() === 'object';
}

function IsNumber(obj) {
    return isType(obj).toLowerCase() === 'number';
}

function IsBoolean(obj) {
    return isType(obj).toLowerCase() === 'boolean';
}

function IsUndefined(obj) {
    return isType(obj).toLowerCase() === 'undefined';
}

function IsFunction(obj) {
    return isType(obj).toLowerCase() === 'function';
}

function IsArray(obj) {
    return isType(obj).toLowerCase() === 'array';
}

function IsDate(obj) {
    return isType(obj).toLowerCase() === 'date';
}

function IsRegExp(obj) {
    return isType(obj).toLowerCase() === 'regexp';
}


function IsEmpty(obj) {
    if (IsUndefined(obj)) {
        return IsUndefined(obj)
    } else if (IsNull(obj)) {
        return IsNull(obj)
    } else if (IsArray(obj)) {
        return obj.length <= 0
    } else if (IsString(obj)) {
        return obj === ""
    } else if (IsObject(obj)) {
        return Object.keys(obj) == 0
    } else if (IsNumber(obj)) {
        return false
    } else if (IsBoolean(obj)) {
        return false
    }
}

/**
 * 
 * @param {*} val 值
 * @param {*} index 位置
 * @param {*} str 插入字符串
 */
// 将手机号转化为*** **** ****
function Insert(val, index, str) {
    var strArray = val.split("");
    if (index < strArray.length) {
        strArray[index] = str + strArray[index];
    }
    return strArray.join("");
}

function Trim(str, c) {
    return trimRight(trimLeft(str, c), c);
}

function trimLeft(str, c) {
    // eslint-disable-next-line no-useless-escape
    return str.replace(new RegExp("^" + (c || "\s\s") + "*"), '');
}

function trimRight(str, c) {
    let ret = str, i = ret.length;
    //用该 RegExp 对象在一个字符串中检索时，将寻找的是字符 "e"
    // eslint-disable-next-line no-useless-escape 
    while (new RegExp(c || '\s').test(ret.charAt(--i)));

    return ret.slice(0, i + 1);
}

//获取cookie、
function GetCookie(name) {
    let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (document.cookie.match(reg)) {
        let arr = document.cookie.match(reg)
        return (arr[2]);
    }
    else {
        return null;
    }
}

//设置cookie,增加到vue实例方便全局调用
function SetCookie(c_name, value, expiredays) {
    let exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

//删除cookie
function DelCookie(name) {
    let exp = new Date();
    exp.setTime(exp.getTime() - 1);
    let cval = GetCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

function IphoneScore() {
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        //软键盘收起的事件处理
        setTimeout(() => {
            const scrollHeight = document.documentElement.scrollTop || document.body.scrollTop || 0
            window.scrollTo(0, Math.max(scrollHeight - 1, 0));
        }, 100)
    }
}


export {
    TransormObjKey,
    IsNull,
    IsString,
    IsObject,
    IsNumber,
    IsBoolean,
    IsUndefined,
    IsFunction,
    IsArray,
    IsDate,
    IsRegExp,
    IsEmpty,
    GetCookie,
    SetCookie,
    DelCookie,
    Insert,
    Trim,
    IphoneScore
}